package com.bailan.foreat.controller;

import com.bailan.foreat.entity.User;
import com.bailan.foreat.service.CartService;
import com.bailan.foreat.utils.JSONUtils;
import com.bailan.foreat.utils.UserUtils;
import com.bailan.foreat.vo.CartAll;
import com.bailan.foreat.vo.CartItem;
import com.bailan.foreat.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    private CartService cartService;


    /**
     * 将 购物车 按照店铺分类
     * @param request
     * @return 所有购物车内容，无价格计算
     */
    @RequestMapping("getAllCartVos")
    @ResponseBody
    private List<CartVo> getAllCartVos(HttpServletRequest request){
        User currentUser = UserUtils.getCurrentUser(request);
        return cartService.getAllCartVos(currentUser.getId());
    }

    @RequestMapping("calTotalPrice")
    @ResponseBody
    public BigDecimal calTotalPrice(HttpServletRequest request, @RequestParam("productIds[]") Integer[] productIds) {
        User currentUser = UserUtils.getCurrentUser(request);
        return cartService.calTotalPrice(currentUser.getId(), productIds);
    }

    /**
     * 按照传入的productId数组，返回购物车信息，此方法也可当作价格计算
     * @param request
     * @param productIds
     * @return
     */
    @RequestMapping("calPrice")
    @ResponseBody
    public CartAll calPrice(HttpServletRequest request, @RequestParam("productIds[]")Integer[] productIds,@RequestParam(defaultValue = "true") Boolean excludeOthers) {
        User currentUser = UserUtils.getCurrentUser(request);
        return cartService.calPrice(currentUser.getId(), productIds,excludeOthers);
    }



    @RequestMapping("updateBuyCount")
    @ResponseBody
    public String updateBuyCount(HttpServletRequest request,Integer productId,Integer buyCount) {
        //获取当前用户id
        User currentUser = UserUtils.getCurrentUser(request);
        return cartService.updateBuyCount(currentUser.getId(),productId,buyCount);
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(Integer productId,HttpServletRequest request) {
        User userByAccount = UserUtils.getCurrentUser(request);
        cartService.addCart(userByAccount.getId(), productId);
        return "加购成功！";
    }

    @RequestMapping("toOrderPreview")
    public String toOrderPreview(Integer[] productIds, Model model) {
        model.addAttribute("productIds", JSONUtils.createJson(productIds));
        return "userorderpreviewbyel";
    }


    /**
     * 防止购买量<库存，更新redis同步数据库
     * @param request
     * @return
     */
    @RequestMapping("updateCart")
    @ResponseBody
    public Boolean updateCart(HttpServletRequest request) {
        //获取当前用户id
        User currentUser = UserUtils.getCurrentUser(request);
        return cartService.updateCart(currentUser.getId());
    }



}
